#ifndef _MODBUS_H
#define _MODBUS_H


#include "main.h"
#include <stdint.h>
#include "stm32f429xx.h"
#define INVALID_DATA -9999

#pragma  pack(1)

typedef union {
	uint8_t buff[8];
	struct {
		uint8_t 	addr;
		uint8_t 	opcode;
		uint16_t 	reg;
		uint16_t	reg_len;
		uint16_t	crc16;
	} pkg;
}modbus_req_t;

typedef union {
	uint8_t buff[7];
	struct {
		uint8_t 	addr;
		uint8_t 	opcode;
		uint8_t 	data_len;
		uint16_t	data;
		uint16_t	crc16;
	} pkg;
}modbus_rep_t;


#pragma pack ()

enum modbus_analy_status {
	MOD_REQ_ADDR,
	MOD_REQ_OPCODE,
	MOD_REQ_DATA,
	MOD_REP_PKG,
	MOD_BUF_CLEAR,
	MOD_DMA_CLEAR_END,
	MOD_ANALY_END,
};

int rs485_send(uint8_t*pData ,uint16_t iLen); //485发送数据
int rs485_read_slave(uint8_t*pData ,uint16_t iRecvLen); //从机485接收数据
float getCtrlPanelEnvTemp( unsigned char addr); //获得面板环境温度
int getCtrlPanelStatus( unsigned char addr); //获得控制面板状态
float getCtrlPanelSetTemp( unsigned char addr) ;//读取面板设定温度
int getCtrlPanelUnit( unsigned char addr);  //得到面板单位
//int set_panel_temp( unsigned char addr, float fValue) ;//设定温度
int set_panel_status( unsigned char addr, int iValue) ; //设定状态
int checkModbus(unsigned char *buff, int len); //主设备检查有效性
int analyPackage(unsigned char *buf, int len); //分析包，解析
void slave_uart_recv_process(USART_TypeDef *pUart);
int set_panel_temp( unsigned char addr, float Value);


#endif
